/*
Copyright (c) 2010, Funius
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the Funius nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY Funius ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Funius BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.funius.xgn;

import java.util.List;
import java.util.Map;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import org.apache.struts2.interceptor.SessionAware;

import com.funius.PersistenceFactory;

public class RegisterAction implements SessionAware {

	private String user;
	private String pass;
	private String error;
	
	private Map<String,Object> sesion;
	
	@SuppressWarnings("unchecked")
	public User findUserByEmail(String userName){
		PersistenceManager pm = PersistenceFactory.getPersistenceManager();
		String queryStr="select from com.funius.xgn.User where mail=='"+userName+"'";
		try{
			Query query = pm.newQuery(queryStr);
			List<User> lst=(List<User>)query.execute(query);
			if ((lst!=null) && (lst.size()>0)){
				return lst.get(0);
			}
			return null;
		}finally{
			pm.close();
		}
	}
	
	public String execute() {
		if ((user==null) || (pass==null) || (user.length()==0) || (pass.length()==0)){
			return "error";
		}
		
		User usuario=findUserByEmail(user);
		if (usuario!=null){
			error="El usuario ya existen";
			return "error";
		}
		
		usuario=new User();
		usuario.setMail(user);
		usuario.setPassword(pass);
		sesion.put("USER", usuario);
		return "success";
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.sesion=session;
	}


	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPass() {
		return pass;
	}

	public void setPass(String pass) {
		this.pass = pass;
	}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}
}
